package com.smile.publisher.service.Impl;

import com.smile.publisher.service.ESService;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:smile
 * Date:2022/2/16
 * Desc:操作ES接口的实现类
 */
@Service
public class ESServiceImpl implements ESService {
    //将ES的客户端操作对象注入到Service中
    @Autowired
    JestClient jestClient;

    /* es 查询语句
    GET /smile1483_dau_info_2022-02-15-query/_search
    {
    "query": {
    "match_all": {}
     }
   }
     */

    //获取当天日活量
    @Override
    public Long getDauTotals(String date) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(new MatchAllQueryBuilder());
        String query = searchSourceBuilder.toString();
        String index = "smile1483_dau_info_"+date+"-query";
        //System.out.println(index);
        Search searchBuild = new Search.Builder(query)
                .addIndex(index)
                .build();

        Long total = 0L;
        try {
            SearchResult result = jestClient.execute(searchBuild);
            total = result.getTotal();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("查询ES失败");
        }
        return total;
    }
    //获取当天某个时刻的日活量
    /*
      "aggs": {
      "groupBy_hour": {
      "terms": {
        "field": "hr",
        "size": 24
     */
    @Override
    public Map<String, Object> getDauHourTotals(String date) {
        Map<String, Object> map = new HashMap<>();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(
                "group_hr", ValueType.LONG).field("hr").size(24);
        searchSourceBuilder.aggregation(termsAggregationBuilder);
        String query = searchSourceBuilder.toString();
        String index = "smile1483_dau_info_" + date + "-query";
        Search build = new Search.Builder(query)
                .addIndex(index)
                .build();
        try {
            SearchResult result = jestClient.execute(build);
            TermsAggregation termsAggregation = result.getAggregations().getTermsAggregation("group_hr");
            if(termsAggregation != null){
                List<TermsAggregation.Entry> buckets = termsAggregation.getBuckets();
                for (TermsAggregation.Entry busket:buckets) {
                   map.put(busket.getKey(),busket.getCount());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("查询ES失败");
        }
        return map;
    }
}
